<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addArtinfo">添加分类</el-button>
      </div>
      <el-table
      border
    :data="cartList"
    style="width: 100%">
    <el-table-column
      label="编号"
      width="180"
      prop="id">
    </el-table-column>
    <el-table-column
    prop="cate_name"
      label="分类名称"
      width="500">
    </el-table-column>
        <el-table-column
    prop="cate_alias"
      label="分类别名"
      width="500">
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="editDel(scope.row)">
          编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="artDel(scope.row)">
          删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 添加分类弹出层 -->
  <el-dialog
  :title="artTitle"
  :visible.sync="dialogVisible"
  width="35%"
  :closed="handleClose">
  <el-form ref="artCateRef" :model="artCateForm" :rules="rulesArtCate" label-width="80px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="artCateForm.cate_name"></el-input>
  </el-form-item>
  <el-form-item label="分类标签" prop="cate_alias">
    <el-input v-model="artCateForm.cate_alias"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doneEdit">确 定</el-button>
  </span>
</el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      // 用户添加修改转换标题
      artTitle: '',
      cartList: [],
      dialogVisible: false,
      //   添加分类的表单项
      artCateForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      //  验证添加分类表单的正则
      rulesArtCate: {
        cate_name: [
          { required: true, message: '请输入文章分类标题', tigger: 'blur' },
          { min: 1, max: 10, message: '标题长度必须是1~10位之间字符', tigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入文章分类别名', tigger: 'blur' },
          { pattern: /^[a-zA-Z]{1,15}$/, message: '别名是1~15位非空字符串', tigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async  initCartList () {
      //  请求文章分类的数据
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return
      this.cartList = res.data
    },
    // 添加文章分类
    addArtinfo () {
    // 弹出添加时用到的对话框
      this.dialogVisible = true
      this.artTitle = '添加文章分类'
    },
    // 删除文章标题
    artDel (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('该分类不允许被删除！')
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        // 发起删除请求
        this.$http.delete('/my/cate/del', {
          params: { id: row.id }
        }).then(res => {
          if (res.data.code !== 0) return
          this.initCartList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑文章分类
    editDel (row) {
      // 弹出添加时用到的对话框
      this.dialogVisible = true
      //   修改标题
      this.artTitle = '修改文章分类'
      //   渲染
      this.artCateForm.cate_name = row.cate_name
      this.artCateForm.cate_alias = row.cate_alias
      //  id赋值
      this.artCateForm.id = row.id
    },
    handleClose () {
      // 清空表单并关闭
      this.$refs.artCateRef.resetFields()
      this.dialogVisible = false
    },
    // 修改添加按钮按下
    doneEdit () {
      // 兜底校验
      this.$refs.artCateRef.validate(async valid => {
        if (!valid) return
        if (this.artTitle === '添加文章分类') {
        //  发送添加请求
          const { data: res } = await this.$http.post('/my/cate/add', this.artCateForm)
          if (res.code !== 0) return
          // 刷新列表
          this.initCartList()
          // 重置表单
          this.$refs.artCateRef.resetFields()
          // 关闭对话框
          this.dialogVisible = false
        } else {
          // 发送修改请求
          const { data: res } = await this.$http.put('/my/cate/info', this.artCateForm)
          if (res.code !== 0) return
          // 刷新列表
          this.initCartList()
          // 重置表单
          this.$refs.artCateRef.resetFields()
          // 关闭对话框
          this.dialogVisible = false
        }
      })
    }
  },
  created () {
    //   初始化
    this.initCartList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
